package com.giser.testapp.lib.Push;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.giser.testapp.MainActivity;
import com.giser.testapp.lib.App.GlobClass;

import cn.jpush.android.api.JPushInterface;

import static android.content.ContentValues.TAG;

/**
 * Created by Lss on 2017/12/7.
 * 自定义接收器
 *
 * 如果不定义这个 Receiver，则：
 * 1) 默认用户会打开主界面
 * 2) 接收不到自定义消息
 */

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
                Log.d(TAG, "[MyReceiver] 用户点击打开了通知");
                Intent appIntent = new Intent(Intent.ACTION_MAIN);
                String message = intent.getExtras().getString(JPushInterface.EXTRA_ALERT);
                appIntent.putExtra(GlobClass.PushMessage,message);
                if(GlobCheck.CheckProgressIsRunning(context, GlobClass.PackageName)){
                    if(GlobCheck.getCurrentPackageName(context).equalsIgnoreCase(GlobClass.PackageName)){
                        //EnvLog.i("other="+GlobCheck.getCurrentPackageName(context));
                        appIntent.setClass(context, MainActivity.class);
                        appIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED|
                                Intent.FLAG_ACTIVITY_CLEAR_TOP
                                |Intent.FLAG_ACTIVITY_SINGLE_TOP );
                        appIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                        //appIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                        context.startActivity(appIntent);
                    }else{
                        //EnvLog.i("en="+GlobCheck.getCurrentPackageName(context));
                        String myN=GlobCheck.getMySystemName(context);
                        if(myN!=""){
                            appIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                            try {
                                appIntent.setComponent(new ComponentName(context, Class.forName(myN)));
                                appIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
                                        | Intent.FLAG_ACTIVITY_NEW_TASK
                                        | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
                                        |Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                context.startActivity(appIntent);
                            } catch (ClassNotFoundException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                        }
                    }

                }else{
                    appIntent.setClass(context, MainActivity.class);
                    appIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED|
                            Intent.FLAG_ACTIVITY_CLEAR_TOP
                            |Intent.FLAG_ACTIVITY_SINGLE_TOP );
                    appIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                    //appIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                   // EnvLog.i("222222");
                    context.startActivity(appIntent);
                }

            }
        } catch (Exception e){

        }
    }
}
